package com.yl.module.property.controller;

import com.yl.common.core.validate.Group;
import com.yl.module.file.domain.FileType;
import com.yl.module.file.domain.vo.File2VO;
import com.yl.module.file.service.FileService;
import com.yl.module.property.domain.dto.JiankePushDataDTO;
import com.yl.module.property.domain.dto.PropertyAddOrUpdDTO;
import com.yl.module.property.domain.dto.PropertyEnableIdleDTO;
import com.yl.module.property.domain.dto.PropertyPageDTO;
import com.yl.module.property.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import com.yl.module.property.service.PropertyService;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.yl.common.core.domain.Result;
import javax.validation.constraints.NotNull;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 资产表 Controller控制层
 * @author makejava
 * @date 2024-10-23 15:54:15
 */
@Slf4j
@RestController
@Api(tags = "资产")
@RequestMapping("/property")
@Validated
public class PropertyController {
    /**
     * 资产表 Service
     */
    @Autowired
    public PropertyService propertyService;
	@Autowired
    public FileService fileService;

    @GetMapping("/getPage")
    @ApiOperation(value = "分页查询", notes = "")
	public Result<Page<PropertyVO>> getByPage(
		@Validated PropertyPageDTO dto
	) {
		Page<PropertyVO> result = propertyService.getPage(dto);
		return Result.ok(result);
	}

    @GetMapping("/getByKey")
    @ApiOperation(value = "通过主键查询", notes = "")
    @ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "int"),
	})
	public Result<PropertyVO> getByKey(
			@NotNull(message = "参数id不能为空")Integer id
	) {
		PropertyVO result = propertyService.getByKey(id);
		return Result.ok(result);
	}

	@PostMapping("/file")
	@ApiOperation(value = "资产封面图片文件上传", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
	})
	public Result<File2VO> fileCover(
			@RequestParam(name = "file") MultipartFile file
	) {
		File2VO result = fileService.upload(FileType.TypeEnum.FILE_TYPE_1000.getCode(),file);
		return Result.ok(result);
	}

	@PostMapping("/add")
    @ApiOperation(value = "添加", notes = "")
	public Result<Integer> add(
			@RequestBody @Validated(Group.Add.class) PropertyAddOrUpdDTO dto
	) {
		Integer result = propertyService.add(dto);
		return Result.ok(result);
	}

	@PostMapping("/upd")
    @ApiOperation(value = "修改", notes = "")
	public Result upd(
			@RequestBody @Validated(Group.Upd.class) PropertyAddOrUpdDTO dto
	) {
		propertyService.upd(dto);
		return Result.ok();
	}

	@GetMapping("/statistics")
	@ApiOperation(value = "统计", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "propertyTypeId", value = "资产一级分类id 1家电 2安消 3家具 4构件", required = true, dataType = "Long"),
	})
	public Result<PropertyStatisticsVO> statistics(
			@RequestParam(name = "propertyTypeId") Integer propertyTypeId
	) {
		PropertyStatisticsVO result = propertyService.statistics(propertyTypeId);
		return Result.ok(result);
	}

	@GetMapping("/getPropertyUpkeepMaintain")
	@ApiOperation(value = "查询单个资产的维保运维记录", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "propertyId", value = "资产主键id （propertyId和ueCode 不能同时为空）", required = false, dataType = "int"),
			@ApiImplicitParam(name = "ueCode", value = "ue编码 （propertyId和ueCode 不能同时为空）", required = false, dataType = "String"),
	})
	public Result<List<PropertyUpkeepMaintainVO>> getPropertyUpkeepMaintain(
			@RequestParam(name = "propertyId",required = false) Integer propertyId,
			@RequestParam(name = "ueCode",required = false) String ueCode
	) {
		List<PropertyUpkeepMaintainVO> result = propertyService.getPropertyUpkeepMaintain(propertyId,ueCode);
		return Result.ok(result);
	}

	@PostMapping("/enableIdle")
	@ApiOperation(value = "资产启用与闲置", notes = "")
	public Result enableIdle(
			@RequestBody PropertyEnableIdleDTO dto
	) {
		propertyService.enableIdle(dto);
		return Result.ok();
	}

	//http://139.159.181.65:50000/lzj/yl/property/jiankePushData
	@PostMapping("/jiankePushData")
	@ApiOperation(value = "建科推送智能资产的数据", notes = "")
	public Result<List<PropertyUpkeepMaintainVO>> jiankePushData(
			@RequestBody @Validated JiankePushDataDTO dto
	) {
		/*
		测试json
			当type为1时 表示是否在线的数据
				{
					"type": 1,
					"signature":"lz//V33AT7tnluSmrtfEoRvnhTcG5vIct2YIc4rblxW3vIlK0rBGb1QYkp5Nykk5ePF3unOas+z/lUhWHQM0/MmsnXLm6KDpYzd5IP5aEVotRPn+8SbKKygKP3mp8yfUPDvzKXtENut0uc7qCXPl42lHHbfUkXlMpcnwjHB1hxM=",
					"propertyUniqueCode": "17308011723340f9a165604ab5bc9363",
					"data": "{\"state\":0}"
				}

			当type为2时 表示是否告警的数据
				{
					"type": 2,
					"signature":"lz//V33AT7tnluSmrtfEoRvnhTcG5vIct2YIc4rblxW3vIlK0rBGb1QYkp5Nykk5ePF3unOas+z/lUhWHQM0/MmsnXLm6KDpYzd5IP5aEVotRPn+8SbKKygKP3mp8yfUPDvzKXtENut0uc7qCXPl42lHHbfUkXlMpcnwjHB1hxM=",
					"propertyUniqueCode": "17308011723340f9a165604ab5bc9363",
					"data": "{\"state\":0}"
				}

			当type为3时 表示设备属性参数
				{
					"type": 3,
					"signature":"lz//V33AT7tnluSmrtfEoRvnhTcG5vIct2YIc4rblxW3vIlK0rBGb1QYkp5Nykk5ePF3unOas+z/lUhWHQM0/MmsnXLm6KDpYzd5IP5aEVotRPn+8SbKKygKP3mp8yfUPDvzKXtENut0uc7qCXPl42lHHbfUkXlMpcnwjHB1hxM=",
					"propertyUniqueCode": "100000000001",
					"data": "{\"deviceReads\":[{\"keyName\":\"温度\",\"value\":\"32°\",\"key\":\"temperature\"},{\"keyName\":\"湿度\",\"value\":\"55\",\"key\":\"humidity\"}]}"
				}

			当type为4时 设备操作数据 灯开关
				{
					"type": 4,
					"signature":"lz//V33AT7tnluSmrtfEoRvnhTcG5vIct2YIc4rblxW3vIlK0rBGb1QYkp5Nykk5ePF3unOas+z/lUhWHQM0/MmsnXLm6KDpYzd5IP5aEVotRPn+8SbKKygKP3mp8yfUPDvzKXtENut0uc7qCXPl42lHHbfUkXlMpcnwjHB1hxM=",
					"propertyUniqueCode": "17308011725940c019289d3f4dce136f",
					"data": "{\"deviceDataType\": 1,\"operationData\": [{\"key\": \"switch_on\",\"keyName\": \"灯的开关\",\"value\": \"0\"}]}"
				}

			当type为4时 设备操作数据 窗帘开合
				{
					"type": 4,
					"signature":"lz//V33AT7tnluSmrtfEoRvnhTcG5vIct2YIc4rblxW3vIlK0rBGb1QYkp5Nykk5ePF3unOas+z/lUhWHQM0/MmsnXLm6KDpYzd5IP5aEVotRPn+8SbKKygKP3mp8yfUPDvzKXtENut0uc7qCXPl42lHHbfUkXlMpcnwjHB1hxM=",
					"propertyUniqueCode": "30093978",
					"data": "{\"deviceDataType\": 2,\"operationData\": [{\"key\": \"opener_current\",\"keyName\": \"窗帘的开合\",\"value\": \"70\"}]}"
				}

		 */
		propertyService.jiankePushData(dto);
		return Result.ok();
	}

	@GetMapping("/getUeList")
	@ApiOperation(value = "ue-初始化列表", notes = "")
	public Result<List<PropertyUeVO>> getUeList(
	) {
		List<PropertyUeVO> result = propertyService.getUeList();
		return Result.ok(result);
	}

	@GetMapping("/getPropertyInfo")
	@ApiOperation(value = "获取资产详情信息", notes = "")
	public Result<PropertyInfoVO> getPropertyInfo(
			@RequestParam(value = "ueCode") String ueCode
	) {
		PropertyInfoVO result = propertyService.getPropertyInfo(ueCode);
		return Result.ok(result);
	}

	@PostMapping("/importZhuJian")
	@ApiOperation(value = "数据导入 第一步 住建运营提供的数据 主要导入智能资产基础数据")
	//https://docs.qq.com/sheet/DQ2hvSUdETEtERG5F?tab=000001
	public Result<String> importZhuJian(
			@RequestParam(value = "file") MultipartFile file
	) {
		propertyService.importZhuJian(file);
		return Result.ok();
	}

	@PostMapping("/importBim")
	@ApiOperation(value = "数据导入 第二步 BIM工程师提供的数据 主要导入非智能资产基础数据、智能非智能类型数据")
	public Result<String> importBim(
			@RequestParam(value = "file") MultipartFile file
	) {
		propertyService.importBim(file);
		return Result.ok();
	}

	@PostMapping("/importFile")
	@ApiOperation(value = "数据导入 文件数据导入")
	public Result<String> importFile(
			@RequestParam(value = "file") MultipartFile file
	) {
		propertyService.importFile(file);
		return Result.ok();
	}

	@GetMapping("/maintainChangeState/test")
	@ApiOperation(value = "测试-保养+更换事件给冯江的模型进行推送", notes = "")
	public Result<Result> maintainChangeState(
	) {
		propertyService.maintainChangeState();
		return Result.ok();
	}
}

